package com.cwj.mansysback.servlet.impl;

import com.cwj.mansysback.entity.Account;
import com.cwj.mansysback.entity.LocalAccount;
import com.cwj.mansysback.entity.Student;
import com.cwj.mansysback.entity.Teacher;
import com.cwj.mansysback.service.api.AccountService;
import com.cwj.mansysback.service.api.StudentService;
import com.cwj.mansysback.service.api.TeacherService;
import com.cwj.mansysback.service.impl.AccountServiceImpl;
import com.cwj.mansysback.service.impl.StudentServiceImpl;
import com.cwj.mansysback.service.impl.TeacherServiceImpl;
import com.cwj.mansysback.servlet.filter.LockFilter;
import com.cwj.mansysback.servlet.util.MyUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Locale;

public class GetSomeAccServlet extends HttpServlet {


    private static final AccountService accSer = new AccountServiceImpl();

    private static final TeacherService teaSer = new TeacherServiceImpl();

    private static final StudentService studSer = new StudentServiceImpl();

    private static final String keys[] = {"accrank","pageInd","rowCount","orderKey","orderType"};

    static class GetSomeAccStatement extends Statement{
        List<Teacher> teachers;
        List<Student> students;
        List<Account> accounts;

        public Long getCount() {
            return count;
        }

        public void setCount(Long count) {
            this.count = count;
        }

        Long count = 0L;

        public GetSomeAccStatement(Integer statue) {
            super(statue);
        }

        public GetSomeAccStatement(Integer statue, List<Teacher> teachers, List<Student> students, List<Account> accounts) {
            super(statue);
            this.teachers = teachers;
            this.students = students;
            this.accounts = accounts;
        }

        public List<Teacher> getTeachers() {
            return teachers;
        }

        public void setTeachers(List<Teacher> teachers) {
            this.teachers = teachers;
        }

        public List<Student> getStudents() {
            return students;
        }

        public void setStudents(List<Student> students) {
            this.students = students;
        }

        public List<Account> getAccounts() {
            return accounts;
        }

        public void setAccounts(List<Account> accounts) {
            this.accounts = accounts;
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        LocalAccount loAcc= LockFilter.localAccount;

        String aid = loAcc.getAid();
        Integer loRank = loAcc.getAccrank();




        if( req.getParameter(keys[0]) == null || req.getParameter(keys[1]) == null || req.getParameter(keys[2]) == null){
            MyUtils.writeResponse(resp,new Statement(8));
            return ;
        }

        Integer accrank = Integer.valueOf(req.getParameter(keys[0]));

        int pageInd = Integer.parseInt(req.getParameter(keys[1]));

        int rowCount = Integer.parseInt(req.getParameter(keys[2]));




        String orderKey = req.getParameter(keys[3]);

        String orderType = req.getParameter(keys[4]);

        boolean isAsc = orderKey == null || !orderType.toLowerCase(Locale.ROOT).equals("desc");


        if(!loRank.equals(Account.RankName.ADMIN.getRank()) && accrank < loRank ){

            MyUtils.writeResponse(resp,new Statement(6));

            return ;
        }

        GetSomeAccStatement statement =new GetSomeAccStatement(0);

        Long count = 0L;

        if(accrank.equals(Account.RankName.TEACHER.getRank())){
            List<Teacher> res = teaSer.selectSomeTeacher(pageInd,rowCount,(orderKey == null ?"\ttid\t": orderKey),isAsc);

            for (Teacher re : res) {
                re.lastChange();
            }

            statement.setTeachers(res);

            count = teaSer.selectAllPack().getCount();


        }else if(accrank.equals(Account.RankName.STUDENT.getRank())){
            List<Student> res = studSer.selectStud(pageInd,rowCount,(orderKey == null ?"\tsid\t": orderKey),isAsc);

            for (Student re : res) {
                re.lastChange();
            }

            statement.setStudents(res);

            count = studSer.selectAllPack().getCount();

        }else{
            statement = new GetSomeAccStatement(4);
        }

        statement.setCount(count);


        MyUtils.writeResponse(resp,statement);



    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
